import myComponent from '../alert/alert.vue'

export default {
    data () {
        return {
            page: 0,
            listItem: [],
            listMore: true,
            loadlock: true,
        }
    },
    mounted() {
        this.loading();
        this.scrolling();
        window.scrollTo(0,0);
    },
    components: {
        myComponent
    },
    methods: {
        loading(){
            var _this = this;
            if (_this.loadlock) {
                _this.loadlock = false;
                _this.page += 1;
                _this.$http.post('/index.php?s=/EyesDsj/json4list',{page:_this.page}).then(function(res){
                    var data = res.data.data;
                    if (data.length > 0) {
                        for (var i in data) {
                            _this.listItem.push(data[i]);
                        }
                    } else {
                        _this.listMore = false;
                    }
                    _this.loadlock = true;
                });
            }
        },
        scrolling(){
            var _this = this;
            window.onscroll = function () {
                if(document.getElementById('more')){
                    if (_this.listMore) {
                        var top = window.screen.availHeight + document.body.scrollTop;
                        var more = document.getElementById('more').offsetTop;
                        if (top >= more) {
                            _this.loading();
                        }
                    }
                }
            }
        },
        vote(id,index){
            var _this = this;
            _this.$http.post('/index.php?s=/EyesDsj/vote',{id:id}).then(function(res){
                var data = res.data;
                _this.$refs.alerts.showalert(data.errmsg, '谢谢您的参与');
                if(data.errmsg == '投票成功'){
                    _this.listItem[index].votecount = parseInt(_this.listItem[index].votecount)+1;
                }
            });
        }
    }
}
